#include "utils/file_utils.hpp"
#include <fstream>
#include <filesystem>
#include <iostream>


bool FileUtils::saveFile(const std::string& filepath, const std::string& content) {
    return saveFile(filepath, content.c_str(), content.size());
}


bool FileUtils::saveFile(const std::string& filepath, const char* data, size_t size) {
    std::ofstream file(filepath, std::ios::binary);

    if (!file) {
        return false;
    }

    file.write(data, size);
    return file.good();
}


std::string FileUtils::readFile(const std::string& filepath) {
    std::ifstream file(filepath, std::ios::binary);
    if (!file) {
        return "";
    }

    return std::string((std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());
}


bool FileUtils::fileExists(const std::string& filepath) {
    return std::filesystem::exists(filepath);
}


bool FileUtils::createDirectory(const std::string& path) {
    return std::filesystem::create_directories(path);
}

long long FileUtils::getFileSize(const std::string& filepath) {
    try {
        return std::filesystem::file_size(filepath);
    } catch (...) {
        return -1;
    }
}
